package view.controller;

import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import logic.FileSystem;
import view.PropertyWindow;

import java.io.IOException;

public class NewPropertyController {

    FileSystem fileSystem = FileSystem.getInstance();

    @FXML
    private VBox vb2 = new VBox();

    @FXML
    private GridPane grid;

    public NewPropertyController(){}

    public void initialize() throws IOException {
        Button button;

        for(int i=0;i<128;i++){
            button = (Button) grid.getChildren().get(i);
            int finalI = i;

            button.setOnMouseClicked(e->{
                try {
                    ControllerTool controllerTool = new ControllerTool("PropertyWindow.fxml");
                    Parent parent = controllerTool.getParent();
                    PropertyWindowController controller = (PropertyWindowController) controllerTool.getController();

                    PropertyWindow.display(parent,controller, finalI);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }

            });
        }
    }

    public void flush(Event event) {
        int size = vb2.getChildren().size();
        for (int i=0;i<size;i++){
            vb2.getChildren().remove(0);
        }
        int index = 0;
        for (int i : fileSystem.fat) {
            vb2.getChildren().add(new Label(String.valueOf(index++)+"   :   "+String.valueOf(i)));
        }

        index = 0;
        for(int i=0;i<128;i++){
            if(fileSystem.fat[i]!=0){
                grid.getChildren().get(i).setStyle("-fx-background-color: red");
                //System.out.println(i);
            }
        }
    }
}
